package com.hx.er.action.admin.settings;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.detail.CityDetailBean;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminSettingsService;

public class AdminCityEditAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String CITY_NAME_UNIQUE_MSG = "admin.cityList.message.edit.unique.cityName";
	public static final String CITY_NAME_EMPTY_MSG = "admin.cityList.message.edit.empty.cityName";
	public static final String CITY_NAME_NOT_LETTERS = "admin.cityList.message.edit.letters.cityName";
	public static final String LOCAL_CITY_NAME_EMPTY_MSG = "admin.cityList.message.edit.empty.localCityName";

	private long cityId;
	private CityDetailBean cityDetailBean;

	@Autowired
	private IAdminSettingsService adminSettingsService;

	public String init() {
		this.initPage();
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			return INPUT;
		}

		this.cityDetailBean.setCityId(this.cityId);
		this.cityDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		// 插入数据
		int count = this.adminSettingsService.updateCity(this.cityDetailBean);

		if (count == 0) {
			this.initPage();
			this.addActionError(super.getText(EDIT_FAILURE_MSG));
			return INPUT;
		} else {
			this.addActionMessage(super.getText(EDIT_SUCCESS_MSG));
			return SUCCESS;
		}
	}

	private void initPage() {
		// 查询餐馆信息
		this.cityDetailBean = this.adminSettingsService.getCityDetailById(this.cityId);
	}

	private boolean checkParams() {
		// 城市英文名验证
		if (StringUtil.isEmpty(this.cityDetailBean.getCityName())) {
			this.addFieldError("msg.cityName", super.getText(CITY_NAME_EMPTY_MSG));
		} else if (!StringUtil.isCharsLetter(this.cityDetailBean.getCityName())) {
			this.addFieldError("msg.cityName", super.getText(CITY_NAME_NOT_LETTERS));
		}

		// 城市本地名验证
		if (StringUtil.isEmpty(this.cityDetailBean.getLocalCityName())) {
			this.addFieldError("msg.localCityName", super.getText(LOCAL_CITY_NAME_EMPTY_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public CityDetailBean getCityDetailBean() {
		return cityDetailBean;
	}

	public void setCityDetailBean(CityDetailBean cityDetailBean) {
		this.cityDetailBean = cityDetailBean;
	}

}
